﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Nissen_Model;

namespace Nissen_Dao
{
	public class MarkableQuestionDao:DaoBase
	{
		public void SaveOrUpdateMarkableQuestions(IList<MarkableQuestion> markableQuestions, string SubjectCourseID, string SubjectCoursePage)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				using (ITransaction t1 = session.BeginTransaction())
				{
					ICriteria crt = session.CreateCriteria<MarkableQuestion>();

					crt.Add(Restrictions.Eq("SubjectCourseID", SubjectCourseID));

					crt.Add(Restrictions.Eq("SubjectCoursePage", SubjectCoursePage));

					IList<MarkableQuestion> dbMarkableQuestions = crt.List<MarkableQuestion>();

					foreach (var dbMarkableQuestion in dbMarkableQuestions)
					{
						session.Delete(dbMarkableQuestion);
					}

					foreach (var markableQuestion in markableQuestions)
					{
						session.Save(markableQuestion);
					}

					t1.Commit();
				}

			}
		}

		public IList<MarkableQuestion> GetMarkableQuestions(string subjectCourseTestId, string pageNum)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<MarkableQuestion>();

				crt.Add(Restrictions.Eq("SubjectCourseID", subjectCourseTestId));

				crt.Add(Restrictions.Eq("SubjectCoursePage", pageNum));

				return crt.List<MarkableQuestion>();
			}
		}
	}
}
